package com.woniuxy02;

import java.util.Scanner;

/**
 * @author 作者
 * @create 2025-08-29 14:23
 */
public class Test03 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String answer;
        int num;
        int[] array = {1,3,45,12,5,0,0,0};
        int beginIndex;
        int endIndex;
        do{
            endIndex=-1;
            System.out.println("输入要添加的元素");
            num=sc.nextInt();
            System.out.println("输入要添加的元素的位置");
            beginIndex=sc.nextInt();
            //查找第一个空位:查找第一个元素为0的下标位置
            for(int i=0;i<array.length;i++){
                if(array[i]==0){
                    endIndex=i;
                    break;
                }
            }

            if(endIndex==-1){
                System.out.println("扩容");
                int[] newArray=new int[array.length+1];
                for (int i = 0; i < array.length; i++) {
                    newArray[i]=array[i];
                }
                array=newArray;
                endIndex=array.length-1;
            }

            //移动元素
            for(int i=endIndex; i>=beginIndex+1; i--){
                array[i]=array[i-1];
            }
            //插入添加的元素
            array[beginIndex]=num;

            for(int i:array){
                if(i==0){
                    break;
                }
                System.out.print(i+"\t");
            }
            System.out.println();
            System.out.println("是否继续添加y/n");
            answer = sc.next();
        }while (answer.equals("y"));

    }
}
